<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class CommController extends Controller {
	//留言列表
    public function commlist(){
    	$res = DB::table('comment_datas')->paginate(5);
    	$rs=['res'=>$res];

        return view("admin\mail-inbox",$rs);
    }
    //右边框架集留言文章显示
    public function you($commentid = 0){
    	if ($commentid === 0) {
    		$res = DB::table('comment_datas')->orderBy('commentid','desc')->take(1)->first();
    	}else{
    		$res = DB::table('comment_datas')->where('commentid',$commentid)->first();
    	}
    	$rs=['res'=>$res];
        return view("admin\you",$rs);
    }
    //留言的加星和删除
    public function youPost(Request $req , $commentid = 0){
    	if (isset($req->delete)) {
    		$rs = DB::table('comment_datas')->where('commentid',$commentid)->delete();
	    	return $rs ? '删除成功' : '删除失败';
    	}  
    	if (isset($req->status)) {
    		$rs = DB::table('comment_datas')->where('commentid',$commentid)->update(['status'=>1]);
	    	return $rs ? '加星成功' : '加星失败';
    	}
    }
    //留言页中间框架集
     public function centre($style = 0){
     	if ($style==="xing") {
     		$res = DB::table('comment_datas')->where("status",1)->paginate(5);
     	}else if($style==="new"){
            $res = DB::table('comment_datas')->orderBy('commentid','desc')->paginate(5);
        }else if($style==="lose"){
            $res = DB::table('comment_datas')->where("status",'-1')->paginate(5);
        }else if($style==="normal"){
            $res = DB::table('comment_datas')->where("status",'0')->paginate(5);
        }else{
     		$res = DB::table('comment_datas')->paginate(5);
     	}
    	$rs=['res'=>$res];
        return view("admin\mail-z",$rs);
    }

    public function centrePost(Request $req , $commentid = 0){
        //$aaa是穿过来的数组值
        if ($_POST['cal']=='del') {
            //批量删除
            $aaa = array_filter(explode(',',$_POST['shu']));
            $rs = DB::table('comment_datas')->whereIn('commentid',$aaa)->delete();
            return $rs ? '删除成功' : '删除失败';
        }else if ($_POST['cal']=='lose') {
            //批量设为垃圾
            $aaa = array_filter(explode(',',$_POST['shu']));
            $rs = DB::table('comment_datas')->whereIn('commentid',$aaa)->update(['status'=>'-1']);
            return $rs ? '设置成功' : '设置失败';
        }
        
    
    }
}
